use strict;
package Data::Form::Field::Option;

use base qw(Data::Form::Field::Base);
use Data::Dumper;
our @attributes = qw( @options options_label );

__PACKAGE__->mk_accessors(@attributes);

our $VERSION = '0.01_01';

#===============================================================================
# Object and Data Initilization
#

sub new {
	my $class		= shift;
	my %params = @_;
	my $obj		= $class->SUPER::new( %params );
	$obj->add_options( $params{options} ); 
	return $obj
}


sub default {
    my ( $self, $options ) = @_;
    return $self->get('default') unless $options;
    return $self->set('default', _opt($options) );
}

sub add_options {
	my $self	  = shift;
	my $options = shift;
	
	# This creates and returns the options needed based
    # on an $opt array/hash shifted in
    
    my @opt;

	push @opt, { value => $self->options_label, label => $self->options_label } if $self->options_label;

    if ( ref($options) eq 'HASH' ) {
        while ( my ( $i, $v ) = each %$options ) {
            push @opt, { value => $i, label => $v };
        }
    }
    elsif ( ref($options) eq 'ARRAY' ) {
            push ( @opt, { value => $_, label => $_ }) for @$options;
    }
	
	$self->options( @opt );
}

#sub _opt {

    # This creates and returns the options needed based
    # on an $opt array/hash shifted in
#    my $options = shift;
#    my @opt;

#    if ( ref($options) eq 'HASH' ) {
#        while ( my ( $i, $v ) = each %$options ) {
#            push @opt, { id => $i, value => $v };
#        }
#    }
#    elsif ( ref($options) eq 'ARRAY' ) {
#            push ( @opt, { id => $_, value => $_ }) for @$options;
#    }
#
#    return @opt;
#
#}

#===============================================================================
# Validation Routines
#

sub validate {
    my $self = shift;
    $self->validate_options unless $self->SUPER::validate;
}

sub validate_options {
    my $self	= shift;
	my $valid	= 0;
	my $v    	= '';

	# is it in the option list?
	my $valid_values = 0;
	my $num_values = 0;
    for $v ( $self->value ) 
    {
    	$num_values++;
      	for my $option ( $self->options  ) 
      	{
      		if ($option->{value} eq $v )
      		{ 
      		 	$valid_values++;
      		 	last;
      		}
      	}
    }
    
    $valid = 1 if $valid_values == $num_values;
    #print "Preliminary check of values indicates: $valid\n";
    # how do we handle the options_label?
    if ( $self->options_label ) {
    	#print "We have an options_label\n";
    	my @values_contain_option_label = grep { $_ eq $self->options_label } $self->value;
		$valid = @values_contain_option_label ? 0 : 1;
		#print "Options_label has been selected\n";
    	if  ( $self->required and !$valid ){
    		#print "We are required and not valid\n";
    		$self->reset();
    		$valid = 0;
    	}
    	elsif ( $self->required and $valid ){
    		#print "We are required and valid\n";
    		$valid = 1	
    	} 
    	else {
    		#print "Else";
    		$self->value( $self->default ) if $self->default;
    		$valid = 1;
    	}
    }
    
    $self->add_errors( sprintf "NO_SUCH_OPTION", $v) if not $valid;
	$self->valid( $valid ? 1 : 0);
}


1;

__END__